package com.ymttest.database.mongo;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

import org.apache.http.impl.cookie.PublicSuffixFilter;
import org.json.JSONArray;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.database.MongoDBDriver;
import com.ymttest.business.service.AssertService;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

/************************************************************************************
 * @File name :SellerPromotionInfoWapper.java
 * @Author : 赵培培
 * @Date : 2016年10月31日
 * @Copyright : 洋码头
 * 满减促销库
 ************************************************************************************/
public class NewSellerPromotionInfoWapper {
	
	MongoDBDriver mdd;
	public NewSellerPromotionInfoWapper() throws Exception{
		mdd = new MongoDBDriver(EnvSetup.getSystemUrl("set5_mongoip"),27017, "sellerpromotion");
		mdd.setPoolSize(Integer.valueOf(EnvSetup.getSystemUrl("mongoPoolSize")));
		mdd.auth("sellerpromotionuser", "123456");
		mdd.selectCollection("ReachPromotionPolicy");
	}
	/**
	 * 根据促销ID查询促销信息
	 * @param PromotionId
	 * @return{_id=652, sid=103, name=103:满减活动PN, des=103:满减描述3e95a98e-c17d-4712-ad18-2290e939a629, bt=Fri Jun 02 16:53:23 CST 2017, et=Sat Jun 03 16:52:59 CST 2017, type=4, ct=Fri Jun 02 16:53:23 CST 2017, ut=Fri Jun 02 16:53:23 CST 2017, isuc=false, issp=false, pjm=1, em=3, vs=[ { "rv" : "5" , "dv" : "0.1"}]}
	 * @throws Exception
	 */
	public Map<String,Object> getPromotionById(int PromotionId) throws Exception {
		
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"_id\":" + PromotionId + "}",1);
	    
	    return list.size()>0?list.get(0):null;
	}
	/**
	 * 获取买手所有的促销信息,最多查50条
	 * @param SellerId
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getPromotions(int SellerId) throws Exception {
		ArrayList<Map> list = mdd.selectPartLimitToMap("{\"sid\":"+SellerId+"}",50);
	    return list;
	}
	/**
	 * 备用 不清楚何时会有问题
	 * @param SellerId
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getEnablePromotion2(int SellerId) throws Exception {
//		{$or:[{"isend":false},{"isend":null}]}
		String search="{'sid':"+SellerId+",'em':0,'et':{$gte:{$date:'"+YMTDateUtil.getDateForMongoFormat(0, 0, 0)+"'}}}";
		Logger.debug(search);
		ArrayList<Map> list = mdd.selectPartLimitToMap(search,5);
	    return list;
	}
	/**
	 * 查询当前可用直播
	 * @param SellerId
	 * @return [{_id=77, sid=1, name=1:满减活动PN, des=1:满减描述7686e8d7-f0fb-45af-b7f4-c4e871010a0f, bt=Fri May 26 17:58:29 CST 2017, et=Sat May 27 17:57:59 CST 2017, type=1, ct=Fri May 26 17:58:29 CST 2017, ut=Fri May 26 17:58:29 CST 2017, isuc=false, issp=false, pjm=1, isend=false, vs=[ { "rv" : "0" , "dv" : "1"}]}]
	 * @throws Exception
	 */
	public ArrayList<Map> getEnablePromotion(int SellerId) throws Exception {
		BasicDBObject search =new BasicDBObject("et", new BasicDBObject("$gte", YMTDateUtil.getDateBeforeOrNextSecond(0))); 
		search.put("sid", SellerId);
		search.put("em", 0);
		Logger.debug(search);
		ArrayList<Map> list = mdd.selectPartLimitToMap(search,5);
	    return list;
	}
	/**
	 * 查询历史活动,条件为sid对应 em对应
	 * @param SellerId null不过滤
	 * @param em 状态 0 为自然状态 3取消 4提前结束 null不过滤
	 * @param etb et所在的时间段开始
	 * @param ete et所在的时间段开始
	 * @return
	 * @throws Exception
	 */
	public ArrayList<Map> getHistoryPromotion(Integer SellerId,Integer em,Date etb,Date ete) throws Exception {
//		db.getCollection('ReachPromotionPolicy').find({"em":0,"et":{$gt:ISODate("2017-06-04T00:00:00.000+08:00")},"sid":1,"et":{$lt:ISODate("2017-06-08T00:00:00.000+08:00")}})
		BasicDBList values = new BasicDBList();
		BasicDBObject search =new BasicDBObject();
		values.add(new BasicDBObject("et", new BasicDBObject("$gte", etb))); 
		values.add(new BasicDBObject("et", new BasicDBObject("$lt", ete)));
		values.add(new BasicDBObject("et", new BasicDBObject("$lt", YMTDateUtil.getDateBeforeOrNextSecond(0))));
		search.put("$and", values);
		if(SellerId!=null){
			search.append("sid", SellerId);
		}
		if(em!=null){
			search.append("em", em);
		}
		Logger.debug(search);
		ArrayList<Map> list = mdd.selectPartLimitToMap(search,100);
	    return list;
	}
	
	public static void main(String[] args) throws Exception {
		ArrayList<Map> a= new NewSellerPromotionInfoWapper().getHistoryPromotion(1,0,YMTDateUtil.getDateBeforeOrNextSecond(-100*60*60*24),YMTDateUtil.getDateBeforeOrNextSecond(0));
		System.out.println(FormatUtil.beanToGSONString(a));
	}   

}
